package com.demo.spring_demo.config;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

//国际化配置类(参照源码：AcceptHeaderLocaleResolver类)
public class MyLocaleResolver implements LocaleResolver {
    //解析请求
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //1、获取请求中的语言参数
        String language = request.getParameter("l");
        //2.1、如果没有就使用默认值(zh_CN)
        Locale locale = Locale.getDefault();
        //2.1.1 设置默认值(en_US)
        locale.setDefault(Locale.US);
        //2.2、如果请求的连接携带了国际化的参数
        if(!StringUtils.isEmpty(language)){
            //分割
            //zh_CN
            String[] str = language.split("_");
            //国家、地区
            locale=new Locale(str[0],str[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
    
    
}
